package mate.task.odin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.Window;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;


public class LocateActivity extends MapActivity {
	GeoPoint p;

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.map);
		MapView map = (MapView) findViewById(R.id.mapview);
		Bundle b = getIntent().getExtras();
		String s  = (String) b.getSerializable("loc");
		Geocoder geoCoder = new Geocoder(this);
		List<Address> addresses = new ArrayList<Address>();
		
		try {
			addresses = geoCoder.getFromLocationName(s, 5);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(addresses.size() > 0){
			p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int)(addresses.get(0).getLongitude()* 1E6));
		    MapController mc = map.getController();
		    mc.animateTo(p); 
		    int zoomlevel = map.getMaxZoomLevel(); 
		    mc.setZoom(zoomlevel - 4); 

            MapOverlay mapOverlay = new MapOverlay();
          List<Overlay> listOfOverlays = map.getOverlays();
          listOfOverlays.clear();
          listOfOverlays.add(mapOverlay);

            map.invalidate();
            
		}
	     else
	     {
	             AlertDialog.Builder adb = new AlertDialog.Builder(LocateActivity.this);
	             adb.setTitle("Google Map");
	             adb.setMessage("Please Provide the Proper Place");
	             adb.setPositiveButton("Close",null);
	             adb.show();
	     }

		
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	class MapOverlay extends com.google.android.maps.Overlay
	{
	    public boolean draw(Canvas canvas, MapView mapView, 
	    boolean shadow, long when) 
	    {
	        super.draw(canvas, mapView, shadow);                   
	        Point screenPts = new Point();
	        mapView.getProjection().toPixels(p, screenPts);

	        Bitmap bmp = BitmapFactory.decodeResource(
	            getResources(), R.drawable.unchecked);            
	        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-32, null);         
	        return true;
	    }
	}

}


